using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CrudI.Interface;

namespace SQLCodeCreator.DBInfo.SqlServer
{
    public class SqlTable : ITable
    {
        private string _TableName;
        private List<IColumn> _ColumnList;
        private bool _hasPrimaryKey;
        private string _pkField;
        private List<SqlServerConstraint> _constraintList;
        private bool _hasForeignKey;

        public string PkField
        {
            get { return _pkField; }
            set { _pkField = value; }
        }
        public List<IColumn> ColumnList
        {
            get { return _ColumnList; }
        }
        public bool HasPrimaryKey
        {
            get { return _hasPrimaryKey; }
        }
        public bool HasForeignKey
        {
            get { return _hasForeignKey; }
        }
        public string TableName
        {
            get { return _TableName; }
            set { _TableName = CrudI.Util.StringUtils.ConvertFirstLetterToUppercase(value); }
        }

        public List<SqlServerConstraint> ConstraintList
        {
            get { return _constraintList; }
            set { _constraintList = value; }
        }

        public SqlTable(string tableName)
        {
            _constraintList = new List<SqlServerConstraint>();
            _ColumnList = new List<IColumn>();
            _TableName = CrudI.Util.StringUtils.ConvertFirstLetterToUppercase(tableName);
        }
        public SqlTable()
        {
            _ColumnList = new List<IColumn>();
        }

        public void AddColumn(IColumn column)
        {
            _ColumnList.Add(column);

            // if there is not a primary key found check this column
            if (!_hasPrimaryKey)
            {
                if (column.IsPK)
                {
                    _hasPrimaryKey = true;
                    _pkField = column.ColumnName;
                }
            }
        }

        public IColumn GetPrimaryKeyField()
        {
            IColumn pkCol = null;
            for (int i = 0; i < _ColumnList.Count; i++)
            {
                if (_ColumnList[i].IsPK)
                {
                    pkCol = _ColumnList[i];
                    break;
                }
            }
            return pkCol;
        }
        private void SetPrimaryKeyField()
        {
            for (int i = 0; i < _ColumnList.Count; i++)
            {
                if (_ColumnList[i].IsPK)
                {
                    _hasPrimaryKey = true;
                    break;
                }
            }
        }
        public void AddColumns()
        {
            _ColumnList = SqlColumn.GetColumns(TableName);
            SetPrimaryKeyField();
            SetForeignKeyField();
        }

        private void SetForeignKeyField()
        {
            for (int i = 0; i < _ColumnList.Count; i++)
            {
                if (_ColumnList[i].IsFK)
                {
                    _hasPrimaryKey = true;
                    break;
                }
            }
        }
        
    }
}
